package org.hospital.service.impl;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hospital.entity.Employee;
import org.hospital.entity.EmployeeDAO;
import org.hospital.entity.IpDAO;
import org.hospital.service.LoginService;
import org.hospital.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;

@Service
public class LoginServiceImpl implements LoginService {

	@Autowired
	private EmployeeDAO eDao;
	@Autowired
	private IpDAO ipDao;

	@Override
	public Employee login(String account, String pwd, String ip) {
		Employee e = eDao.getByAccountAndPwd(account, pwd);
		if(e == null) {
			return null;
		}
		// 如果允许跨域登录
		if (e.getIsDomain() == 0) {
			e.setLoginTime(new Timestamp(DateUtil.getNowLong()));
			e.setStatus(0);
			e.setLoginIp(ip);
			eDao.merge(e);
			return e;
		}
		

		Map properties = new HashMap();
		properties.put("clinicId", e.getClinicId());
		properties.put("ip", ip);
		List ipList = ipDao.getByProperties(properties);
		if (ipList == null || ipList.size() == 0) {
			return null;
		}
		e.setLoginTime(new Timestamp(DateUtil.getNowLong()));
		e.setStatus(0);
		e.setLoginIp(ip);
		eDao.merge(e);
		return e;
	}

	
	@Override
	public Employee logout(int employeeId) {
		Employee employee = eDao.findById(employeeId);
		
		employee.setStatus(1);
		eDao.merge(employee);
		return null;
	}

}
